#!/bin/bash
#=======================
# Author : Ali Moradzade
# Created: 2022/01/25
#=======================

# daily archive - archive designated files & directories
# ======================================================

# gather current date
DATE=$(date +%y%m%d)

# set archive file name
FILE=archive$DATE.tar.gz

# set configuration and destination file
CONFIG_FILE=~/works/linux-bible/24/FILES-TO-BACKUP
DESTINATION=~/works/linux-bible/24/$FILE

# ======================================================
# main script

# check backup config file exists
if [ -f $CONFIG_FILE ]
then
	echo
else
	echo
	echo "$CONFIG_FILE does not exist."
	echo "Backup not completed due to missing Configuration file."
	echo
	exit
fi

# build the names of all the files to backup
FILE_NO=1
exec < $CONFIG_FILE

read FILE_NAME
while [ $? -eq 0 ]
do
	# make sure the file or directory exists
	if [ -f $FILE_NAME -o -d $FILE_NAME ]
	then
		FILE_LIST="$FILE_LIST $FILE_NAME"
	else
		echo
		echo "$FILE_NAME, does not exist."
		echo "Obviously, I will not include it in this archive."
		echo "It is listed on line $FILE_NO of the config file."
		echo "Continuing to build archive list ..."
		echo
	fi

	FILE_NO=$[ $FILE_NO + 1 ]
	read FILE_NAME
done

# ======================================================
# backup the files and compress archive
echo "Starting archive ..."
echo

tar -czf $DESTINATION $FILE_LIST 2> /dev/null

echo "Archive completed"
echo "Resulting archive file is: $DESTINATION"
echo
